package com.twotiger.and.activity.account.cash;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.BankcardAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.BroswerUrl;
import com.twotiger.and.bean.CashBank;
import com.twotiger.and.util.ViewUtils;

/**
 * 
 * @Description: 
 * 提现时修改银行 
 * @Author douxy
 * @Date 2015年8月12日 下午1:41:11
 */
public class CashBankCardList extends BaseActivity {

    private static final int BINDCARDSUCCCES = 0;
    private static final int BINDCARDFAIL = 1;
    private ListView bank_list_cards;
    private View add_bankcard_layout;
    private HashMap<String, String> params;

    private SubTitle subTitle;
    private BankcardAdapter bankcardAdapter;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.choice_bankcard_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        bank_list_cards = (ListView) view.findViewById(R.id.bank_list_cards);
        add_bankcard_layout = view.findViewById(R.id.add_bankcard_layout);
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("更改提现银行卡");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
    }

    @Override
    protected void processLogic() {
        params = new HashMap<String, String>();
        List<CashBank> bankcardlist = application.getcashBanks();
        bankcardAdapter = new BankcardAdapter(this, bankcardlist);
        bank_list_cards.setAdapter(bankcardAdapter);
    }

    @Override
    protected void onActivityResult(int requestcode, int resultcode, Intent intent) {
        super.onActivityResult(requestcode, resultcode, intent);
        if (requestcode == CashPage.BINGCARDREQUEST) {
            if (resultcode == CashPage.BINGCARDRESULTCLOSE_SUC) {
                List<CashBank> bankcardlist = application.getcashBanks();
                bankcardAdapter.resetBankCardList(bankcardlist);
            }
        }
    }

    @Override
    protected void setListener() {
        add_bankcard_layout.setOnClickListener(this);
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case BINDCARDSUCCCES:
                    Basebean result_bind = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_bind.isOk()) {
                        BroswerUrl broswerurl = JSON.parseObject(result_bind.data, BroswerUrl.class);
                        Intent intent = new Intent(mContext, WebViewPage.class);
                        intent.putExtra("title", "绑定提现银行卡");
                        intent.putExtra("url", broswerurl.getUrl());
                        intent.putExtra("isBindCard", CashPage.BINDCARD);
                        //                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                        startNewActivityforresult(intent, R.anim.push_left_in,
                            R.anim.push_left_out, CashPage.BINGCARDREQUEST);
                    } else {
                        showToast(result_bind.codeDesc);
                    }
                    break;
                case BINDCARDFAIL:
                    showToast("网络错误");
                    break;

                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
    }

    @Override
    protected void onBackPress() {
        setResult(RESULT_CANCELED);
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    private void bindcard() {
        params.clear();
        params.put("token", getToken());
        getDate(params, Constants.BINDBANK, handler, BINDCARDSUCCCES, BINDCARDFAIL, true, true,
            false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.add_bankcard_layout:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            bindcard();
            break;
        default:
            break;
        }
    }

}
